<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JetBrains 激活码生成</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="/css/layui.css" rel="stylesheet">
    <link href="/css/index.css" rel="stylesheet">
</head>
<body>
<div class="layui-bg-gray" style="padding: 16px;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form">
                        <div class="layui-form-item logo-title">
                            <h1>JetBrains 激活配置生成器</h1>
                        </div>
                        <div class="box-container">
                            <div class="layui-form-item">
                                <label class="layui-form-label">许可证</label>
                                <div class="layui-input-block">
                                    <input type="text" name="licenseeName" value="" lay-verify="required"
                                           placeholder="许可证名称" lay-reqtext="请输入许可证名称" autocomplete="off"
                                           class="layui-input" lay-affix="clear">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">授权名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="assigneeName" value="" lay-verify="required"
                                           placeholder="被授权者名称" lay-reqtext="请填写被授权者名称"
                                           autocomplete="off" class="layui-input" lay-affix="clear">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">IDE名称</label>
                                <div class="layui-input-block">
                                    <select name="ideCode" lay-filter="ideCode" lay-search="">

                                    </select>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">可选插件</label>
                                <div class="layui-input-block">
                                    <div id="pluginList"></div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">过期时间</label>
                                <div class="layui-input-block">
                                    <input type="text"  lay-affix="clear"  placeholder="过期时间不建议超过2026年" name="expireDate"  lay-verify="required"  lay-reqtext="请填写过期时间" class="layui-input" id="expireDate" placeholder="yyyy-MM-dd">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <button class="layui-btn layui-btn-fluid layui-btn-red" lay-submit  lay-filter="generate">生成</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="result">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-card-header">power.conf</div>
            <div class="layui-card-body">
                <pre class="layui-code powerconf"></pre>
            </div>
            <div class="layui-card-header">虚拟机属性配置</div>
            <div class="layui-card-body">
                        <pre class="layui-code vmoption">
--add-opens=java.base/jdk.internal.org.objectweb.asm=ALL-UNNAMED
--add-opens=java.base/jdk.internal.org.objectweb.asm.tree=ALL-UNNAMED
-javaagent: 这里填ja-netfilter.jar的路径
                        </pre>
            </div>
            <div class="layui-card-header">激活码</div>
            <div class="layui-card-body">
                <pre class="layui-code license"></pre>
            </div>
        </div>
    </div>
</script>
<script src="/js/layui.js"></script>
<script>
    //JS
    layui.use(function () {
        var element = layui.element;
        var layer = layui.layer;
        var $ = layui.$;
        var transfer = layui.transfer;
        var form = layui.form;
        var laydate = layui.laydate;
        var power;
        var postCommonObj = {
            assigneeEmail: "",
            licenseRestriction: "",
            checkConcurrentUse: false,
            metadata: "0120230102PPAA013009",
            hash: "41472961/0:1563609451",
            gracePeriodDays: 7,
            autoProlongated: true,
            isAutoProlongated: true
        }
        laydate.render({
            elem: '#expireDate',
        });

        let powerUrl = '/conf/power.conf'
        request(powerUrl, "GET", "", function (res) {
            power = res
        })

        let ideUrl = '/json/ide.json'
        request(ideUrl, "GET", "", function (res) {
            let html = ''
            for (let i = 0; i < res.length; i++) {
                html += '<option value="' + res[i].code + '">' + res[i].name + '</option>'
            }
            $("select[name='ideCode']").html(html)
            form.render('select')
        })
        // 渲染
        transfer.render({
            id: 'pluginList',  //定义唯一索引
            elem: '#pluginList',  //绑定元素
            title: ['待选', '已选'],  //自定义标题
            data: [],
            showSearch: true, // 是否显示搜索框
            parseData: function (res) { // 解析成规定的 data 格式
                return {
                    "value": res.code, // 数据值
                    "title": res.name, // 数据标题
                    "disabled": false,  // 是否禁用
                    "checked": false // 是否选中
                };
            },
            text: {
                none: '无数据', // 数据为空时的提示文本
                searchNone: '无匹配数据' // 搜索无匹配数据时的提示文本
            },
            width: 500, // 定义宽度
            height: 350 // 定义高度
        });

        let pluginUrl = '/json/plugin.json'
        request(pluginUrl, "GET", "", function (res) {
            transfer.reload('pluginList', {
                data: res
            })
        })

        form.on('submit(generate)',function (data){
            var filed = data.field;
            var pluginList = transfer.getData('pluginList');
            var product = [{code:filed.ideCode,fallbackDate:filed.expireDate,paidUpTo:filed.expireDate}];
            for (var i in pluginList) {
                product.push({code:pluginList[i].value,fallbackDate:filed.expireDate,paidUpTo:filed.expireDate})
            }
            postCommonObj.licenseeName = filed.licenseeName;
            postCommonObj.assigneeName = filed.assigneeName;
            postCommonObj.products = product;
            let url = '/api/generate'
            request(url, "POST", JSON.stringify(postCommonObj), function (res) {
                if (res.code === 0) {
                    layer.msg('生成成功', {icon: 1});
                    result(res);
                } else {
                    layer.msg('生成失败', {icon: 2});
                }
            })
            return false;
        })

        function result(res) {
            layer.open({
                type: 1,
                title: '结果',
                content: $('#result').html(),
                offset: 'auto',
                area: ['800px', '600px'],
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    layui.code({
                        elem: '.powerconf',
                        preview:false,
                        code: power,
                        text: {
                            title: 'power.conf',
                            preview: 'power.conf'
                        },
                        wordWrap:false,
                        header:true,
                        theme: 'dark',
                    });
                    layui.code({
                        elem: '.vmoption',
                        preview:false,
                        text: {
                            title: '虚拟机配置',
                        },
                        header:true,
                        theme: 'dark',
                    });
                    layui.code({
                        elem: '.license',
                        preview:false,
                        code: res.data.license,
                        text: {
                            title: 'license',
                        },
                        header:true,
                        theme: 'dark',
                        wordWrap:false,
                    });
                }
            });
        }

        function request(url, method, data, success, error) {
            $.ajax({
                url: url,
                type: method,
                data: data,
                success: success,
                error: error
            });
        }

    });
</script>
</body>
</html>